﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;
using System.Data.SqlClient;

namespace WindowsFormsApplication2.All_FORMS.Lavlah_forms
{    
    public partial class OrderXT : Form
    {
        SqlConnection conn;
        string id1, Cusid1,xt1;
        
        public OrderXT(SqlConnection co, string id, string cusid)
        {
            InitializeComponent();
            conn = co;
            id1 = id;
            Cusid1 = cusid;
        }

        private void OrderXT_Load(object sender, EventArgs e)
        {
            if (Cusid1 =="")
            {
                string aa = string.Format("select *from Orders where {0}='{1}' and {2}=N'{3}'", "XTolologchId", id1, "OrderStatusId", "Идэвхигүй");
                dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa);
                FormatGrid();
            } else
            {
                string aa = string.Format("select *from Orders where {0}='{1}' and {2}=N'{3}' and {4}=N'{5}'", "XTolologchId", id1, "OrderStatusId", "Идэвхигүй", "CustomerId", Cusid1);
                dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa);
                FormatGrid();
            }
            
        }
        public void FormatColumn(String colname, String caption, int index)
        {
            dataGridViewOrderXT.Columns[colname].HeaderText = caption;
            dataGridViewOrderXT.Columns[colname].DisplayIndex = index;
            dataGridViewOrderXT.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatGrid()
        {
            dataGridViewOrderXT.AllowUserToOrderColumns = true;
            dataGridViewOrderXT.AllowUserToAddRows = false;
            dataGridViewOrderXT.AllowUserToDeleteRows = false;
            dataGridViewOrderXT.ReadOnly = true;
            dataGridViewOrderXT.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewOrderXT.MultiSelect = true;

            FormatColumn("OrderId", "Захиалгын дугаар", 0);
            FormatColumn("OrderAwsanOgnoo", "Захиалга авсан огноо", 1);
            FormatColumn("OrderHvrgehOgnoo", "Захиалга хүргэх огноо", 2);
            FormatColumn("OrderStatusId", "Захиалгын төлөв", 3);
            FormatColumn("ProductId", "Бүтээгдэхүүний код", 4);
            FormatColumn("ProductToo", "Тоо хэмжээ", 5);
            FormatColumn("XTolologchId", "Худалдааны төлөөлөгчийн код", 6);
            FormatColumn("CustomerId", "Харилцагчийн код", 7);
            FormatColumn("TolborId", "Төлбөрийн хэлбэр", 8);
        }
        private void dataGridViewOrderXT_MouseClick(object sender, MouseEventArgs e)
        {
            int i = 0;

            i = dataGridViewOrderXT.CurrentRow.Index;
            DataRowView drv = dataGridViewOrderXT.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string orderid = drv[0].ToString();
                string orderhvrgehognoo = drv[1].ToString();
                string Orderawsan = drv[2].ToString();
                string orderStatus = drv[3].ToString();
                string productid = drv[4].ToString();
                string protoo = drv[5].ToString();
                string xt = drv[6].ToString();
                string tolborid = drv[7].ToString();
                textBox1.Text = orderid;
                dateTimePicker1.Text = Orderawsan;
                dateTimePicker2.Text  = orderhvrgehognoo;
                txtAName.Text = orderStatus;
                textBox2.Text = productid;
                textBox4.Text = protoo;
                textBox5.Text = tolborid;
                xt1= xt;
              
            }
        }

        private void btnNextPayment_Click(object sender, EventArgs e)
        {
         
             string aa2= string.Format("update Orders set OrderStatusId = N'Идэвхитэй' where {0}='{1}'", "OrderId", textBox1.Text);
            dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa2);



            if (Cusid1 == "")
            {
                string aa = string.Format("select *from Orders where {0}='{1}' and {2}=N'{3}'", "XTolologchId", id1, "OrderStatusId", "Идэвхигүй");
                dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa);
                FormatGrid();
            }
            else
            {
                string aa = string.Format("select *from Orders where {0}='{1}' and {2}=N'{3}' and {4}=N'{5}'", "XTolologchId", id1, "OrderStatusId", "Идэвхигүй", "CustomerId", Cusid1);
                dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa);
                FormatGrid();
            }
            
            textBox1.Text = "";
            dateTimePicker1.Text = "";
            dateTimePicker2.Text = "";
            txtAName.Text = "";
            textBox2.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                string aa111 = string.Format("delete from Orders where OrderId = '{0}'", textBox1.Text);
                dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa111);
                string aa = string.Format("select *from Orders where {0}='{1}'", "XTolologchId", id1);
                dataGridViewOrderXT.DataSource = DBTools.selecQryTmp(aa);

            }
            else { MessageBox.Show("Алдаа"); }
            
        }
    }
}
